# -*- coding: utf-8 -*-

"""
Module implementing showData1.
"""

from PyQt5.QtCore import pyqtSlot,QDate
from PyQt5.QtWidgets import QDialog,QAbstractItemView,QHeaderView,QMessageBox
from excel2Sqlite import read2Sqlite
from Ui_showData1 import Ui_Dialog
from PyQt5.QtGui import QIcon
import datetime
from OprationExcel import ReadExcel

class showData1(QDialog, Ui_Dialog):
    """
    Class documentation goes here.
    """
    def __init__(self, parent=None):
        """
        Constructor
        
        @param parent reference to the parent widget
        @type QWidget
        """
        super(showData1, self).__init__(parent)
        self.setupUi(self)
        self.setWindowIcon(QIcon('excel.ico'))
        self.setWindowTitle('数据浏览')
        self.control_Init()

    def control_Init(self):
        today = datetime.date.today()
        self.dtStart.setDate(QDate(today.year,today.month,today.day))
        self.dtEnd.setDate(QDate(today.year, today.month, today.day))
        check=self.chkYear.isChecked()
        self.chkYearStatus(check)

    def chkYearStatus(self,check):
        '''
        根据checkbox判断日期空间的可编辑状态
        :param check:chkYear是否勾选
        :return:None
        '''
        self.dtStart.setEnabled(check)
        self.dtEnd.setEnabled(check)

    @pyqtSlot()
    def on_btnQuery_clicked(self):
        """
        Slot documentation goes here.
        """
        name=self.txtName.text()

        if self.chkYear.isChecked():
            dtStart=self.dtStart.text()
            dtEnd=self.dtEnd.text()
            if dtStart>dtEnd:
                QMessageBox.information(self,'提示','开始年月不能大于截止年月')
                self.dtStart.setFocus()
                return
        else:
            dtStart=''
            dtEnd=''
        read2sql=read2Sqlite()
        result, model= read2sql.getCbxx(name,dtStart,dtEnd)
        if result==True:
            if model:
                self.tvData.setModel(model)
                self.tvData.setEditTriggers(QAbstractItemView.NoEditTriggers)
                #self.tvData.horizontalHeader().setStretchLastSection(True)
                self.tvData.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
                #self.tvData.verticalHeader().setVisible(False)      #隐藏行号
                self.tvData.verticalHeader().hide()
                self.tvData.model()
        else:
            #print(model)
            QMessageBox.critical(self,'错误','查询出现错误，原因是：{}'.format(str(model)))
    
    @pyqtSlot()
    def on_btnCancel_clicked(self):
        """
        Slot documentation goes here.
        """
        # TODO: not implemented yet
        self.close()
    
    @pyqtSlot(bool)
    def on_chkYear_clicked(self, checked):
        """
        Slot documentation goes here.
        
        @param checked DESCRIPTION
        @type bool
        """
        self.chkYearStatus(checked)
    
    @pyqtSlot()
    def on_btnExp_clicked(self):
        """
        Slot documentation goes here.
        """
        dataModel=self.tvData.model()

        if dataModel:
            if dataModel.item(0, 0).text()=='无数据！':
                QMessageBox.information(self,'提示','查询无数据，无法导出！')
                return

            rs=ReadExcel()
            result,message=rs.write_Excel(dataModel)
            if result==True:
                QMessageBox.information(self,'提示','导出Excel文件成功！')
            else:
                QMessageBox.critical(self,'错误','导出Excel文件出错，原因是：{}'.format(message))

            # print("{}行，{}列".format(dataModel.rowCount(),dataModel.columnCount()))
            # data=dataModel.item(0,0)
            # print(data.text())
            # for r in range(dataModel.rowCount()):
            #     for c in range(dataModel.columnCount()):
            #         print(dataModel.item(r,c).text())
        else:
            QMessageBox.information(self, '提示', '查询无数据，无法导出！')
            return